package com.thinkTank.oa.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thinkTank.oa.entity.Subscribe;
import com.thinkTank.oa.service.ConferenceService;
import com.thinkTank.oa.service.CustomerService;
import com.thinkTank.oa.service.DemandService;
import com.thinkTank.oa.service.PersonService;
import com.thinkTank.oa.service.ProjectService;
import com.thinkTank.oa.service.SubscribeService;
import com.thinkTank.oa.util.Json;
import com.thinkTank.oa.util.StrUtil;

/***
 * @date 2018-08-29
 * @author dq
 * 用户关注，项目，需求 专家等等
 *
 */
@RequestMapping("/subscri")
@Controller
public class SubscribeController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(BaseController.class);

	@Autowired
	private SubscribeService subscribeService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private DemandService demandService;
    @Autowired
    private PersonService personService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ConferenceService conferenceService;

    /***
     * @date 2018-08-30
     * @author dq
     * @param objectType 模块pid
     * @param type 模块类型
     * @param request
     * @return
     * 关注，项目等
     */
	@RequestMapping(value="addSubscribe" ,method = RequestMethod.POST)
	@ResponseBody
	public Json addSubscribe(String objectType,Integer type,HttpServletRequest request) {
		try{
			Subscribe sub = subscribeService.findSubByUserIdAndType(this.getUserId(request), objectType,type);
			if(null!=sub){
				return new Json(false, "success", "您已经关注,请不要重复关注！");
			}else{
				 sub = new Subscribe();
				 sub.setPid(StrUtil.getUUID32());
				 sub.setSubType(type);
				 sub.setUserid(this.getUserId(request));
				 sub.setTypeObject(objectType);
				 sub.setAddTime(StrUtil.getCurrentDateTime());
				 /**
					 * 关注类型  1.项目  2.需求 3.客户 4.专家 5.会议
					 */
				 if(type==1){
					 sub.setRemark1(projectService.getById(objectType).getProjectName());
				 }else if(type==2){
					 sub.setRemark1(demandService.getById(objectType).getDemandName());
				 }else if(type==3){
					 sub.setRemark1(customerService.getById(objectType).getCustomerName());
				 }else if(type==4){
					 sub.setRemark1(personService.getById(objectType).getName());
				 }else if(type==5){
					 sub.setRemark1(conferenceService.getById(objectType).getConferenceName());
				 }
					
				subscribeService.add(sub);
				
			}
			return new Json(true, "success", "关注成功！");
		}catch(Exception e){
			return new Json(false, "fail", "关注失败,请联系管理员！");
		}
		
		
	}
	/**
	 * @date 2018-08-30
	 * @author dq
	 * @param subId
	 * @return
	 * 删除关注项目
	 */
	@RequestMapping(value="deleteSubscribe" ,method = RequestMethod.GET )
	@ResponseBody
	public Json deleteSubscribe(String subId){
		try{
			subscribeService.delete(subId);
			return new Json(true, "success", "取消关注成功");
		}catch(Exception e){
			return new Json(false, "fail", "取消关注失败,请联系管理员！");
		}
		
	}
	
	
	@RequestMapping(value="deleteSubscribe")
	@ResponseBody
	public Json deleteSubscribe(String objectType,Integer type,HttpServletRequest request){
		try{
			Subscribe sub = new Subscribe();
			sub.setTypeObject(objectType);
			sub.setSubType(type);
			sub.setUserid(getUserId(request));
			subscribeService.deleteByObj(sub);
			return new Json(true, "success", "取消关注成功");
		}catch(Exception e){
			return new Json(false, "fail", "取消关注失败,请联系管理员！");
		}
		
	}
	/**
	 * 查询当前用户的关注列表
	 * @author dq
	 * @date 2018-08-30
	 * @return
	 */
	@RequestMapping(value="showSubscribe" ,method = RequestMethod.GET )
	@ResponseBody
	public Json showSubscribe (Integer pageNo,HttpServletRequest request){
		try{
				PageHelper.startPage(pageNo, 6);
				List<Subscribe> subList = subscribeService.findAllSubByUserId(this.getUserId(request));
				PageInfo<Subscribe> pageInfo = new PageInfo<Subscribe>(subList);
				return new Json(true, "success", pageInfo.getList());
				
				
			
		}catch(Exception e){
			logger.equals(e);
		}
		return new Json(false, "fail", null);
	}
}
